﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.Algorithm;
using Microhard.Core.DBUtility;
using Microhard.Framework.MVCHelper;

namespace Microhard.Permission.MVCFilter
{
    public class BeforeSaveUpdateActionFilter : IActionDataFilter
    {
        #region IActionDataFilter Members

        public virtual bool FilterAction(System.Web.Mvc.ActionExecutingContext filterContext, Models.User currentUser, List<Models.Permission> currentPermissions, Core.DBUtility.ScopeResult dataScope)
        {
            var model = filterContext.ActionParameters["model"];
            var pkid = AlgCompare.GetPKID<ModelPKIDAttribute>(model);
            this.PKID = pkid;
            var rtn = dataScope.PKID.Contains(pkid);
            if (dataScope.Scope == DataCollectionScope.All
                || dataScope.Scope == DataCollectionScope.Custome)
            {
                rtn = true;
            }
            if (IsAdminAction)
            {
                rtn = true;
            }
            if (rtn)
            {
                var pbm = model as BasePermissionModel;
                if (pbm != null)
                {
                    pbm.OperationStatus = OperationStatus.Update;
                }
            }
            return rtn;
        }

        public Guid PKID
        {
            set;
            get;
        }
        #endregion


        public bool IsAdminAction
        {
            set;
            get;
        }
    }
}
